package com.linln.admin.SpringBoot.service.impl;

import com.linln.admin.SpringBoot.domain.Team;
import com.linln.admin.SpringBoot.domain.TeamApply;
import com.linln.admin.SpringBoot.repository.TeamRepository;
import com.linln.admin.SpringBoot.service.TeamService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamRepository teamRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Team getById(Long id) {
        return teamRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Team> getPageList(Example<Team> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return teamRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param team 实体对象
     */
    @Override
    public Team save(Team team) {
        return teamRepository.save(team);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return teamRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public List<Team> getList() {
        List<Team> all = teamRepository.findAll();
        for(Team team1:all){
            if(team1.getType() == 1){
                team1.setTypeName("项目团队");
            }
            if(team1.getType() == 2){
                team1.setTypeName("比赛团队");
            }
            if(team1.getType() == 3){
                team1.setTypeName("其他团队");
            }
        }
        return all;
    }

    @Override
    public List<Team> list() {
        return teamRepository.findAll();
    }

    @Override
    public List<Team> getByTeacherId(Long id) {
        return teamRepository.findByTeacherId(id);
    }

    @Override
    public Team findByTeam(String teamName) {
        return teamRepository.findByTeam_name(teamName);
    }
}